package com.qdxwx.data.service.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.course.CourseNodeMapper;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseNodeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * CourseNode接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CourseNodeServiceImpl implements CourseNodeService {

	@Autowired
	private CourseNodeMapper courseNodeMapper;

	@Override
	public int insert(CourseNode courseNode) {
		courseNode.setAddTime(LocalDateTime.now());
		return courseNodeMapper.insert(courseNode);
	}

	@Override
	public List<CourseNode> getList(int courseId) {
		if (courseId < 1) return new ArrayList<>();
		CourseNode courseNode = new CourseNode();
		courseNode.setCourseId(courseId);
		return courseNodeMapper.selectList(courseNode, null);
	}

	@Override
	public int update(CourseNode courseNode) {
		return courseNodeMapper.update(courseNode);
	}

	@Override
	public int countVideo(int courseId) {
		if (courseId < 1) return 0;
		return courseNodeMapper.countVideo(courseId);
	}

	@Override
	public void updatePlayCount(int nodeId) {
		if (nodeId < 1) return;
		courseNodeMapper.updatePlayCount(nodeId);
	}

	@Override
	public CourseNode getLiving(int courseId, LocalDateTime beginTime) {
		return courseNodeMapper.selectLivingNode(courseId, beginTime);
	}

	@Override
	public List<CourseNodeDTO> getNearestNodeList(CourseNode courseNode, int limit) {
		return courseNodeMapper.selectCourseNearestNodeList(courseNode, limit);
	}

	@Override
	public List<CourseNode> getList(CourseNode courseNode, PageRequest page) {
		return courseNodeMapper.selectList(courseNode, page);
	}

	@Override
	public int count(CourseNode courseNode) {
		return courseNodeMapper.count(courseNode);
	}

	@Override
	public CourseNode get(int nodeId) {
		if (nodeId < 1) return null;
		return courseNodeMapper.selectById(nodeId);
	}

	@Override
	public int delete(int nodeId) {
		return delete(get(nodeId));
	}

	@Override
	public int delete(CourseNode node) {
		if (node == null) return 0;
		int row = courseNodeMapper.deleteById(node.getNodeId());
		if (row < 1) return row;
		/*
		//一并删除相关表和云视频直播等
		if("VIDEO".equals(node.getFileType()) || "AUDIO".equals(node.getFileType())){
			switch (node.getVideoType()) {
				case "BAIJIAYUN":
					BaJiaYunUtils.delVideo(node.getNodeKey(), websiteProfileService.getMap(WebsiteProfileType.baiJiaYun.toString()));
					break;
				case "ALIYUNVIDEO":
					AliyunUtils.deleteVideo(node.getNodeKey(), websiteProfileService.getMap(WebsiteProfileType.aliyun.toString()));
					break;
			}
		}else if("LIVE".equals(node.getFileType())){
			if("BAIJIAYUN".equals(node.getVideoType())){
				BaJiaYunUtils.delLive(node.getNodeKey(), websiteProfileService.getMap(WebsiteProfileType.baiJiaYun.toString()));
			}
		}*/
		return row;
	}

	@Override
	public void deleteByCourseId(int courseId) {
		if (courseId < 1) return;
		List<CourseNode> nodeList = getList(courseId);
		for (CourseNode node : nodeList) {
			delete(node);
		}
	}

	@Override
	public List<CourseNode> getRoots(int courseId) {
		if (courseId < 1) return null;
		return courseNodeMapper.selectRoots(courseId);
	}

	@Override
	public List<CourseNode> getChildren(int parentId) {
		if (parentId < 1) return null;
		return courseNodeMapper.selectChildren(parentId);
	}

	/**
	 * 获取课程章节目录村，改为getTreeByParentId方法即可
	 *
	 * @return 获取课程章节目录村
	 */
	@Override
	public List<CourseNode> getTreeByCourseId(int courseId) {
		List<CourseNode> roots = getRoots(courseId);
		for (CourseNode courseNode : roots) {
			//如果要支持无限级，改为getTreeByParentId方法即可
			courseNode.setChildren(getChildren(courseNode.getNodeId()));
		}
		return roots;
	}

	@Override
	public int getDurationSum(int courseId) {
		if(courseId < 1) return 0;
		return courseNodeMapper.selectDurationSum(courseId);
	}

	private List<CourseNode> getTreeByParentId(int parentId) {
		List<CourseNode> children = getChildren(parentId);
		if (children == null) return null;
		for (CourseNode child : children) {
			child.setChildren(getTreeByParentId(child.getNodeId()));
		}
		return children;
	}
}